/**
 * Collection List
 *  -  user
 *  -  user_followups
 *  -  user_authentications
 *  -  user_community
 */
var Schema = mongoose.Schema;

/**
 * ==================== 用户 - User============================
 */
var userMongoSchema = new Schema({
    userType: {
        type: String,
        enum: ['normalAppUser', 'talent', 'moderator', 'service'],
        default: 'normalAppUser'
    },
    userName: {type: String, unique: true, sparse: true},        //唯一，空值除外
    email: {type: String, unique: true, sparse: true},            //唯一，空值除外
    mobilePhone: {type: String, unique: true, sparse: true},     //唯一，空值除外
    huanxinUserName: {type: String, unique: true},                //唯一，不能为空，环信用户名
    huanxinPassword: {type: String},                                //环信密码
    wechatId: {type: String, unique: true, sparse: true},         //唯一，空值除外，微信token
    userPassword: {type: String, required: true},                 //口令
    nickName: String,                                              //昵称
    /*  宝宝树66小区五期   add  小区大使申请 guo.xiaomin  20171121   start   */
    realName: String, // 真实姓名
    wxName: String, // 微信号
    /*  宝宝树66小区五期   add  小区大使申请 guo.xiaomin  20171121   end   */
    pinyin: String,                                                //昵称拼音
    pinyinShort: String,                                           //昵称拼音首字母
    pinyinSort: String,                                              //拼音排序
    userPortrait: {type: Schema.Types.ObjectId, ref: 'masterData_UserPortrait'},
    frozenOrNot: {type: Boolean, default: false},                   //是否激活
    frozenReason: String,
    /*  宝宝树66小区二次改造    add   guo.xiaomin  20170802   start   */
    gagOrNot:{type: Boolean, default: false},                       //是否禁言
    gagReason:String,
    /*  宝宝树66小区二次改造    add   guo.xiaomin  20170802   end   */
    domainOfExpertise: {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'},//专家类型
    images: [{imageURL: String}],
    maritalStatus: String,                                                     //婚姻状况
    hasChildren: {type: String, enum: ['Yes', 'No', '有', '无']},
    authenticationStatus: {type: Boolean, default: false},                 //认证状态
    school: String,                                                             //毕业院校
    birthDate: Date,                                                           //生日
    speciality: String,                                                        //擅长
    job: String,                                                                //工作
    hobbies:                                                                   //兴趣爱好
        [
            {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'}    //关联到兴趣爱好的schema ID
        ],
    contactBook: [
        {type: Schema.Types.ObjectId, ref: 'user'}                              //通信录
    ],
    age: {type: Number, min: 10, max: 100},                                         //年龄
    gender: {type: String, enum: ['male', 'female', '男', '女']},               //性别
    cityId: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},         //城市
    community: {type: Schema.Types.ObjectId, ref: 'community'},               //所属小区
    apartmentDetail: String,                                                       //详细住址
    roomUnit: String,                                                               //单元
    roomNumber: String,                                                             //房号
    showUnit: {type: Boolean, default: true},                                     //显示单元号
    homeTown: {
        country: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},        //所属国家
        province: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},       //所属省
        city: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},           //所属市
        county: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'}          //所属县
    },
    appSetting: {
        neighborQuestionMessageOn: {type: Boolean, default: true},        //邻居问题推送
        //followupQuestionMessageOn: {type: Boolean, default: true},        //关注问题推送
        //backOfficeMessageOn: {type: Boolean, default: true},              //运营平台推送
        neighborChatMessageOn: {type: Boolean, default: true},            //邻居聊天推送
        //showMeNearOn: {type: Boolean, default: true},                     //附近显示我
        notDisturb: {
            on: {type: Boolean, default: false},
            startTime: String,
            endTime: String
        },
        vibrationOn: {type: Boolean, default: true},
        soundOn: {type: Boolean, default: true}
    },
    clientId: String,
    regChannelId: String,                                               //注册渠道id
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now},
    invitationCode: String,
    invitedBy: {type: Schema.Types.ObjectId, ref: 'user'},
    openId: String,
    loginWay: String,
    userRole: [{type: Number}],           //用户角色  1-版主 2-物业 3-雷锋团 4-创始人
    children: {
        gender: String,
        birthday: String
    },
    remark: String
});
mongoose.model('user', userMongoSchema, 'user');


//===========================用户关注 userFollowup ========================================
var userFollowupSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    category: {type: String, enum: ['question', 'activity', 'user']},
    question: {type: Schema.Types.ObjectId, ref: 'question'},                //关注的问题
    activity: {type: Schema.Types.ObjectId, ref: 'activity'},               //关注的活动
    goods: {type: Schema.Types.ObjectId, ref: 'goods', index: true},
    followUser: {type: Schema.Types.ObjectId, ref: 'user'},                  //关注的用户
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('user_followups', userFollowupSchema, 'user_followups');


//===========================用户认证 user followup ========================================
var userAuthenticationSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    imageURL: String,
    promotionCode: String,
    status: {type: Boolean, default: false},
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now},
    isValid: {type: Boolean, default: true},
    refusalReason: String
});
mongoose.model('user_authentications', userAuthenticationSchema, 'user_authentications');


//===========================用户小区 user community ========================================
var userCommunitySchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    type: {type: Number, default: 1},    // 1,自己小区 2,父母小区
    userRole: [{type: Number}],
    roomUnit: String,
    roomNumber: String,
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now}
});
mongoose.model('user_community', userCommunitySchema, 'user_community');

var inviteWechatBindSchema = new Schema({
    inviteCode: String,
    unionId: String,
    fromUserId: String
});
mongoose.model('invite_wechat_bind', inviteWechatBindSchema, 'invite_wechat_bind');

var inviteScoreRecord = new Schema({
    user: String,
    invited: String,
    source: Number,                 //积分来源
    score: Number,
    reason: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('invite_score_record', inviteScoreRecord, 'invite_score_record');

var recentContactPerson = new Schema({
    userHuanxin: String,
    otherHuanxin: String,
    time: Date
});
mongoose.model('user_recentContactPerson', recentContactPerson, 'user_recentContactPerson');


// 用户申请管理员、雷锋团
var roleApply = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    type: Number,                                            //1：申请管理员  2：申请雷锋团 4：创始人
    city: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    name: String,
    phone: String,
    address: String,
    specialty: [{type: String}],
    status: Number,                                  //0,审核中 1,通过 -1,未通过
    startDateTime:{type: Date}, // 通过大使申请日时
    endDateTime:{type: Date}, // 取消拒绝大使申请日时
    isValid: {type: Boolean, default: true},
    dateCreated: {type: Date, default: Date.now},
    /* 宝宝树66小区 sprint5 创始人申请页面 取消大使修改 modify wangcan 20171207 start */
    cancelReason: String  // 取消大使或拒绝创始人申请原因
    /* 宝宝树66小区 sprint5 创始人申请页面 取消大使修改 modify wangcan 20171207 end */
});
mongoose.model('user_role_apply', roleApply, 'user_role_apply');

/*  宝宝树66小区二次改造    mod   chenf  20170802  start   */
// 大使小区关联表
var ambassadorCommunity = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    isValid: {type: Boolean, default: true},
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now}
});
mongoose.model('ambassador_community', ambassadorCommunity, 'ambassador_community');
/*  宝宝树66小区二次改造    mod   chenf  20170802  end   */

/*  宝宝树66小区二次改造    mod   guo.xiaomin  20170825  start   */
// 小区大使置顶精华屏蔽数据
var moderatorManagerSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'}, // 用户
    topCount: Number,        // 置顶数
    essenceCount: Number,   // 精华数
    shieldingCount: Number  // 屏蔽数
});
mongoose.model('moderatorManager', moderatorManagerSchema, 'moderator_manager');

// 小区大使置顶精华屏蔽操作记录
var moderatorManagerOperationNoteSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'}, // 用户
    question: {type: Schema.Types.ObjectId, ref: 'question'}, // 帖子
    operationType: {type: String, enum: ['置顶', '取消置顶', '精华', '取消精华', '屏蔽', '取消屏蔽']}, // 操作类型
    dateCreated: {type: Date, default: Date.now}, // 创建时间
});
mongoose.model('moderatorManagerOperationNote', moderatorManagerOperationNoteSchema, 'moderator_manager_operation_note');
/*  宝宝树66小区二次改造    mod   guo.xiaomin  20170825  end   */